package com.jzy.gulimall.seckill.scheduled;

import com.jzy.gulimall.seckill.constant.SeckillConstant;
import com.jzy.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author jzy
 * @date 2020/9/23 19:04
 * 秒杀商品定时上架
 * 闲时上架最近三天上架的商品
 */
@Slf4j
@Service
public class SeckillSkuScheduled {

    @Resource
    private SeckillService seckillService;

    @Resource
    private RedissonClient redissonClient;

    /**
     * 闲时上架最近三天上架的商品
     * 定时任务在分布式下的问题 : 分布式锁
     *
     */
    @Scheduled(cron = "0 * * * * ?")
    public void uploadSeckillSkuLastThreeDays() {
        RLock lock = redissonClient.getLock(SeckillConstant.SECKILL_UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        //重复上架无需处理
        try {
            seckillService.uploadSeckillSkuLastThreeDays();
        } finally {
            lock.unlock();
        }
    }

}
